#include "../inc/main.h"
#include "../inc/Client_link.h"

Client_link * link_Client_init(void)
{
    
    Client_link * L = (Client_link *)malloc(sizeof(Client_link));

    L->next = NULL;

    L->len = 0;

    return L;
}


// 插入 -- 尾插
Client_link_node * link_Client_insert(Client_link * L , Client_link_node node)
{   
    if ( NULL == L )
    {
        printf("传入 链表 为空\n");
        return NULL;
    }

    // 建立新结点
    Client_link_node * new_node = (Client_link_node *)malloc(sizeof(Client_link_node));
    if ( NULL == new_node )
    {
        printf("插入 结点 失败\n");
        return NULL;
    }

    *new_node = node;
    new_node->next = NULL;

    // 头插
    if ( 0 == L->len )
    {
        
        L->next = new_node;
    }
    else
    {
        Client_link_node * ptr_node = L->next;

        for (size_t i = 0; i < L->len - 1; i++)
        {
            ptr_node = ptr_node->next;
        }

        ptr_node->next = new_node;
    }

    L->len++;

    return new_node;
}

Client_link_node * link_Client_find_id(Client_link * L ,  int id)
{
    if ( NULL == L )
    {
        printf("传入 链表 为空\n");
        return NULL;
    }

    Client_link_node * ptr = L->next;

    for (size_t i = 0; i < L->len; i++)
    {
        if (ptr->id == id)
        {
            return ptr;
        }
        ptr = ptr->next;
    }


    return NULL;

}

